package com.employ.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.employ.admin.common.Result;
import com.employ.admin.entity.News;
import com.employ.admin.mapper.NewsMapper;
import com.employ.admin.service.NewsService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/news")
public class NewsController {

    @Resource
    NewsService newsService;
    @Resource
    NewsMapper newsMapper;

    @PostMapping
    public Result<?> save(@RequestBody News news){
        newsMapper.insert(news);
        return Result.success();
    }

    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize, @RequestParam(defaultValue = "") String searchStr){
        /**
         * searchStr 空字符串的处理 导入三方库 hutool
         */
        LambdaQueryWrapper<News> wrapper = Wrappers.<News>lambdaQuery();
        if (StringUtils.isNotBlank(searchStr)){//searchStr不为空，再设置为模糊查询
            wrapper.like(News::getTitle, searchStr);
        }
        Page<News> newsPage = newsMapper.selectPage(new Page<>(pageNum,pageSize),wrapper);
        //拿到数据后渲染vue页面，传给tableData
        return Result.success(newsPage);
    }

    @DeleteMapping("/{id}")
    public Result<?> update(@PathVariable Long id){

        newsMapper.deleteById(id);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result<?> getById(@PathVariable Long id){

        newsMapper.selectById(id);
        return Result.success();
    }
}
